Imports System
Imports System.Data.SqlClient
Public Class qLBase
    Private _MyConnect As SqlConnection
    Private _MySqlDA As SqlDataAdapter
    Private _MySqlCmd As SqlCommand
    Private _objDataset As New DataSet
    Public Sub Connection(ByVal sStrConn As String)
        _MyConnect = New SqlConnection
        _MyConnect.ConnectionString = sStrConn
        _MyConnect.Open()
    End Sub
    Public Function MyConnection(ByVal sStrConn As String) As SqlConnection
        MyConnection = New SqlConnection
        MyConnection.ConnectionString = sStrConn
        MyConnection.Open()
        Return MyConnection
    End Function
    Public Sub Disconnect()
        _MyConnect.Close()
    End Sub
    ''' <summary>
    ''' Hàm dùng để lấy dữ liệu từ một bảng
    ''' </summary>
    ''' <param name="StoreName">Tên thủ tục</param>
    ''' <param name="TableName">Tên bảng CSDL</param>
    ''' <returns>Dữ liệu trả về dataset</returns>
    ''' <remarks></remarks>
    Public Function GetData(ByVal StoreName As String, ByVal TableName As String) As DataSet
        _MySqlDA = New SqlDataAdapter
        _MySqlCmd = New SqlCommand(StoreName, _MyConnect)
        _MySqlCmd.CommandType = CommandType.StoredProcedure
        _MySqlDA.SelectCommand = _MySqlCmd
        _MySqlDA.Fill(_objDataset, TableName)
        Return _objDataset
    End Function

    Public Function GetData_BySqlDA(ByVal StoreName As String) As SqlDataAdapter
        Dim oSqlDA As New SqlDataAdapter
        If _MyConnect.State = 0 Then
            _MyConnect.Open()
        End If
        oSqlDA.SelectCommand = New SqlCommand
        oSqlDA.SelectCommand.Connection = _MyConnect
        oSqlDA.SelectCommand.CommandType = CommandType.StoredProcedure
        oSqlDA.SelectCommand.CommandText = StoreName
        Return oSqlDA
    End Function
    ''' <summary>
    ''' Thủ tục thực hiện lệnh sử dụng sqlcommand
    ''' </summary>
    ''' <param name="StoreProcName">Tên thủ tục cần xử lý</param>
    ''' <param name="ArrayParamater">Mảng tham số cần đưa vào</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function RunSqlCommand(ByVal StoreProcName As String, ByVal ArrayParamater() As IDataParameter) As SqlCommand
        If _MyConnect.State = 0 Then
            _MyConnect.Open()
        End If
        _MySqlCmd = New SqlCommand(StoreProcName, _MyConnect)
        _MySqlCmd.CommandType = CommandType.StoredProcedure
        Dim objParamater_ As SqlParameter
        For Each objParamater_ In ArrayParamater
            _MySqlCmd.Parameters.Add(objParamater_)
        Next
        Return _MySqlCmd
    End Function
    ''' <summary>
    ''' Hàm thực thi các lệnh update, insert, delete
    ''' </summary>
    ''' <param name="StoreProcName">Tên thủ tục</param>
    ''' <param name="TableName">Tên bảng CSDL</param>
    ''' <param name="ArrayParamater">Mảng tham số</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function RunStoreProcedures(ByVal StoreProcName As String, ByVal TableName As String, ByVal ArrayParamater() As IDataParameter) As DataSet
        Dim oSqlCommand As New SqlCommand
        oSqlCommand = RunSqlCommand(StoreProcName, ArrayParamater)
        _MySqlDA = New SqlDataAdapter
        _MySqlDA.SelectCommand = oSqlCommand
        _MySqlDA.MissingSchemaAction = MissingSchemaAction.AddWithKey
        _MySqlDA.Fill(_objDataset, TableName)
        _MySqlDA.Dispose()
        Return _objDataset
    End Function
End Class
